home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d902.lha / Less / Source / source.lha / position.c < prev    next >
C/C++ Source or Header  |  1993-01-21  |  3KB  |  129 lines

  1. /*
  2.  * Routines dealing with the "position" table.
  3.  * This is a table which tells the position (in the input file) of the
  4.  * first char on each currently displayed line.
  5.  *
  6.  * {{ The position table is scrolled by moving all the entries.
  7.  *    Would be better to have a circular table
  8.  *    and just change a couple of pointers. }}
  9.  */
  10.  
  11. #include "less.h"
  12.  
  13. #include "position.h"
  14.  
  15. #define NPOS    100             /* {{ sc_height must be less than NPOS }} */
  16. static POSITION table[NPOS];    /* The position table */
  17.  
  18. extern int sc_width, sc_height;
  19.  
  20. /*
  21.  * Return the starting file position of a line displayed on the screen.
  22.  * The line may be specified as a line number relative to the top
  23.  * of the screen, but is usually one of these special cases:
  24.  *      the top (first) line on the screen
  25.  *      the second line on the screen
  26.  *      the bottom line on the screen
  27.  *      the line after the bottom line on the screen
  28.  */
  29. #ifdef __STDC__
  30. POSITION position (int where)
  31. #else
  32.         public POSITION
  33. position(where)
  34.         int where;
  35. #endif
  36. {
  37.         switch (where)
  38.         {
  39.         case BOTTOM:
  40.                 where = sc_height - 2;
  41.                 break;
  42.         case BOTTOM_PLUS_ONE:
  43.                 where = sc_height - 1;
  44.                 break;
  45.         }
  46.         return (table[where]);
  47. }
  48.  
  49. /*
  50.  * Add a new file position to the bottom of the position table.
  51.  */
  52. #ifdef __STDC__
  53. void add_forw_pos (POSITION pos)
  54. #else
  55.         public void
  56. add_forw_pos(pos)
  57.         POSITION pos;
  58. #endif
  59. {
  60.         register int i;
  61.  
  62.         /*
  63.          * Scroll the position table up.
  64.          */
  65.         for (i = 1;  i < sc_height;  i++)
  66.                 table[i-1] = table[i];
  67.         table[sc_height - 1] = pos;
  68. }
  69.  
  70. /*
  71.  * Add a new file position to the top of the position table.
  72.  */
  73. #ifdef __STDC__
  74. void add_back_pos (POSITION pos)
  75. #else
  76.         public void
  77. add_back_pos(pos)
  78.         POSITION pos;
  79. #endif
  80. {
  81.         register int i;
  82.  
  83.         /*
  84.          * Scroll the position table down.
  85.          */
  86.         for (i = sc_height - 1;  i > 0;  i--)
  87.                 table[i] = table[i-1];
  88.         table[0] = pos;
  89. }
  90.  
  91. /*
  92.  * Initialize the position table, done whenever we clear the screen.
  93.  */
  94. #ifdef __STDC__
  95. void pos_clear (void)
  96. #else
  97.         public void
  98. pos_clear()
  99. #endif
  100. {
  101.         register int i;
  102.  
  103.         for (i = 0;  i < sc_height;  i++)
  104.                 table[i] = NULL_POSITION;
  105. }
  106.  
  107. /*
  108.  * See if the byte at a specified position is currently on the screen.
  109.  * Check the position table to see if the position falls within its range.
  110.  * Return the position table entry if found, -1 if not.
  111.  */
  112. #ifdef __STDC__
  113. int onscreen (POSITION pos)
  114. #else
  115.         public int
  116. onscreen(pos)
  117.         POSITION pos;
  118. #endif
  119. {
  120.         register int i;
  121.  
  122.         if (pos < table[0])
  123.                 return (-1);
  124.         for (i = 1;  i < sc_height;  i++)
  125.                 if (pos < table[i])
  126.                         return (i-1);
  127.         return (-1);
  128. }
  129.